<?php
/**
* @filename course_note.php 2016-9-6 02:10:56x
* @author Yunfent Zhang <306232452@qq.com>
* @version 1.0.0
* @copyright DZF (c) 2016, Yunfeng Zhang
*/
header("Content-Type: text/html;charset=utf-8");
class ctrl_course_chapter extends course_chapter {
	public $info_array = array();
	public $page_array = array();
	public $tree_array = array();
	
	static function &instance() {
		static $object;
		if (empty($object)) {
			$object = new self();
		}
		return $object;
	}
	
	/*
	 *	取出一条信息
	 */
	public function do_detail_one($cc_id='') {
		global $_G;
		$detail = array();
		$cc_id = isset($_REQUEST['cc_id']) ? $_REQUEST['cc_id'] : '';
		$detail = $this->one_info('course_chapter','cc_id',$cc_id);
		$api=isset($_REQUEST['api']) ? $_REQUEST['api']:'json';
		//DEBUG 调试输出
		if($detail){
			$return['code'] = '0';
			$return['data']=$detail;
			echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
		}else{
			$return['code'] = '1';
			$return['data']=array();
			echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
		}
	}
	
	/*
	 *	取出课程下的章节列表信息
	 */
	public function do_detail_list() {
		global $_G;
		$detail = array();
		$page = max(1, intval($_GET['page']));
		$perpage = $limit = isset($_REQUEST['limit']) && !empty($_REQUEST['limit']) ? $_REQUEST['limit'] : '';
		$start=(($page-1) * $perpage);
		$wheresql = "";
		$wheresql .= " AND isdelete = 0";
		$api=isset($_REQUEST['api']) ? $_REQUEST['api']:'json';		
		$paging=isset($_REQUEST['paging']) ? $_REQUEST['paging']:0;
		$orderby = " ORDER BY cc_sort,cm_id DESC ";
		$cm_id = isset($_REQUEST['cm_id']) ? $_REQUEST['cm_id'] : '';//课程id
		if($cm_id ){
			$wheresql .= " AND  cm_id = ".$cm_id ;
		}
		//DEBUG 初始化请求请求获取一页列表数据的参数
		$page_condition=array(
				'page' => $page,//int 请求页面 页码
				'limit' => $limit,//int 每页请求个数
				'perpage' => $perpage,//int 每页显示个数
				'wheresql' => $wheresql,//string //条件SQL语句
				'orderby' => $orderby,//string 排序规则
		);
		$page_result = $this->detail_list('course_chapter',$page_condition);
		//DEBUG 调试输出
		if($page_result){
			$return['code'] = '0';
			if($paging==1){
				$return['data']=$page_result;
			}else{
				$return['data']=$page_result['page_data'];
			}			
			echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
		}else{
			$return['code'] = '1';
			$return['data']=array();
			echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
		}
	
	}					
	
	
	/*
	 *  添加章节
	 */
	public function do_add(){
		global $_G;
		$res_data=array();
		$client=isset($_REQUEST['client']) ? $_REQUEST['client']:'pc';
		$course_chapter = isset($_REQUEST['course_chapter']) && !empty($_REQUEST['course_chapter']) ? $_REQUEST['course_chapter']:'';
	
		//TODO 后端字段校验 根据具体业务逻辑添加
		//DEBUG 判断是否数组字段,如果是数组转换为逗号间隔 字符串存贮
		foreach($course_chapter AS $key => $value){
			if(is_array($value) && !empty($value)){
				$course_chapter[$key] = implode(",", $value);
			}
		}
		$course_chapter['create_dateline'] = TIMESTAMP;
		$course_chapter['modify_dateline'] = TIMESTAMP;
		
		$insert_data = $this->add('course_chapter',$course_chapter);
		if($insert_data){
			if($insert_data==='-1'){
				//已有用户购买该课程
				$return['code'] = '4';
				$return['data']=array();
				echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
			}else{
				$return['code'] = '0';
				$return['data']=$insert_data;
				echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
			}
		}else{
			$return['code'] = '1';
			$return['data']=array();
			echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
		}
	
	}
	
	
	
	
	/*
	 *  编辑章节
	 */
	public function do_edit() {
		global $_G;	
		$course_chapter = isset($_REQUEST['course_chapter']) && !empty($_REQUEST['course_chapter']) ? $_REQUEST['course_chapter']:'';		
		$cc_id = isset($_REQUEST['cc_id']) ? $_REQUEST['cc_id'] : '';
		if($cc_id){			
			$course_chapter['modify_dateline'] = TIMESTAMP;
			//DEBUG 判断是否数组字段,如果是数组转换为逗号间隔 字符串存贮
			foreach($course_chapter AS $key => $value){
				if(is_array($value) && !empty($value)){
					$course_chapter[$key] = implode(",", $value);
				}
			}
			$where = array('cc_id'=>$cc_id);
			$effect_row = $this->edit('course_chapter',$course_chapter,$where);
			if($effect_row){
				$return['code'] = '0';
				$return['data']=$course_chapter;
				echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
			}else{
				$return['code'] = '1';
				$return['data']=array();
				echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
			}			
		}else{
			$return['code'] = '3';
			$return['data']=array();
			echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
		}
	}
	
	/*
	 *  删除章节
	 */
	public function do_delete() {		
		global $_G;
		$cc_id = isset($_REQUEST['cc_id']) ? $_REQUEST['cc_id'] : '';
		if($cc_id){
			$effect_row = $this->delete("course_chapter", array("cc_id" => $cc_id));
			if($effect_row){
				if($effect_row==='-1'){					
					//已有用户购买该课程
					$return['code'] = '4';
					$return['data']=array();
					echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
				}else{
					$return['code'] = '0';
					$return['data']=array();			
					echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
				}
			}else{
				$return['code'] = '1';
				$return['data']=array();
				echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
			}
		}else{
			$return['code'] = '3';
			$return['data']=array();
			echo format_data($return,$_G['gp_api'],$_G['gp_mod'],$_G['gp_action'],$_G['gp_do']);
		}
	}
}
?>